import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/extension/ex_widget.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/generated/assets.dart';
import 'package:flutter_massage/pages/technician/report/report_logic.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class ReportPage extends GetView<ReportLogic> {
  const ReportPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      titleStr: '举报',
      resizeToAvoidBottomInset: false,
      child: GetBuilder<ReportLogic>(
        init: ReportLogic(),
        assignId: true,
        builder: (logic) {
          return SingleChildScrollView(
            padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 24.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildReportType(),
                _buildReportDescription().paddingTop(20.w),
                _buildReportPictures().paddingTop(20.w),
                _buildSubmit().paddingTop(53.w),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget _buildReportType() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const TextWidget.oneLine(
          text: '请选择你要举报的类型：',
          fontSize: 15,
          color: AppColors.textLevelOne,
          fontWeight: FontWeight.bold,
        ),
        LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            final double spacing = 10.w;
            final double itemWidth = ((constraints.maxWidth - spacing * 2) / 3).floorToDouble();
            const double borderWidth = 1;
            List<String> reasonList = [
              '违法违规',
              '诈骗信息',
              '时政不实信息',
              '引导站外交易',
              '售卖假货',
              '低差广告',
              '冒充他人',
              '谩骂攻击',
              '疑似自残自杀',
              '其他',
            ];
            return Wrap(
              spacing: spacing,
              runSpacing: 12.w,
              children: List.generate(reasonList.length, (index) {
                bool selected = controller.select == index;
                return ButtonWidget.text(
                  reasonList[index],
                  fontSize: 14,
                  width: itemWidth - borderWidth * 2,
                  height: 36.w - borderWidth * 2,
                  textColor: selected ? '#7150FC'.toColor : AppColors.textLevelOne,
                  borderRadius: 4.w,
                  backgroundColor: selected ? AppColors.white : AppColors.grayF8,
                  alignment: Alignment.center,
                  borderWidth: borderWidth,
                  borderColor: selected ? '#7150FC'.toColor : AppColors.grayF8,
                  onTap: () {
                    controller.select = index;
                    controller.update();
                  },
                  hasInkWell: false,
                );
              }),
            );
          },
        ).paddingTop(16.w),
      ],
    );
  }

  Widget _buildReportDescription() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const TextWidget.oneLine(
          text: '举报描述：（最多300字）',
          fontSize: 15,
          color: AppColors.textLevelOne,
          fontWeight: FontWeight.bold,
        ),
        ClipRRect(
          borderRadius: BorderRadius.circular(8.w),
          child: TextField(
            decoration: const InputDecoration(
              border: InputBorder.none,
              hintText: '提供更多信息有利于举报被快速处理',
              hintStyle: TextStyle(fontSize: 13, color: AppColors.textLevelThree),
              fillColor: AppColors.grayF8,
              filled: true,
            ),
            cursorColor: AppColors.primaryGreen,
            controller: controller.textEditingController,
            maxLines: 6,
            style: const TextStyle(fontSize: 13),
          ),
        ).paddingTop(12.w),
      ],
    );
  }

  Widget _buildReportPictures() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const TextWidget.oneLine(
          text: '图片证据： （最多3张）',
          fontSize: 15,
          color: AppColors.textLevelOne,
          fontWeight: FontWeight.bold,
        ),
        LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            final double spacing = 16.w;
            final double itemWidth = ((constraints.maxWidth - spacing * 2) / 3).floorToDouble();
            return Wrap(
              spacing: spacing,
              children: List.generate(min(controller.images.length + 1, 3), (index) {
                if (index == controller.images.length) {
                  return _buildAddItem(itemWidth);
                }
                return ClipRRect(
                  borderRadius: BorderRadius.circular(8.w),
                  child: Stack(
                    alignment: Alignment.bottomCenter,
                    children: [
                      ImageWidget.file(
                        controller.images[index],
                        fit: BoxFit.cover,
                        width: itemWidth,
                        height: itemWidth,
                        backgroundColor: AppColors.grayF8,
                      ),
                      ButtonWidget(
                        hasInkWell: false,
                        onTap: () {
                          controller.images.remove(controller.images[index]);
                          controller.update();
                        },
                        child: Container(
                          height: 20.w,
                          alignment: Alignment.center,
                          color: AppColors.black.withOpacity(0.4),
                          child: const TextWidget.oneLine(
                            text: '删除',
                            fontSize: 13,
                            color: AppColors.white,
                          ),
                        ),
                      ),
                    ],
                  ).width(itemWidth),
                );
              }),
            );
          },
        ).paddingTop(16.w),
      ],
    );
  }

  Widget _buildAddItem(double size) {
    return ButtonWidget.icon(
      Assets.svgReportAdd.toSvg(width: 28.w),
      width: size,
      height: size,
      backgroundColor: AppColors.grayF8,
      alignment: Alignment.center,
      hasInkWell: false,
      borderRadius: 8.w,
      onTap: () async {
        List<AssetEntity>? images = await ActionPicker.assets(maxAssets: 1);
        if (images != null && images.isNotEmpty) {
          File? file = await images.first.file;
          if (file != null) controller.images.add(file.path);
          controller.update();
        }
      },
    );
  }

  Widget _buildSubmit() {
    return ButtonWidget.text(
      '提交',
      fontSize: 16,
      fontWeight: FontWeight.bold,
      height: 44.w,
      textColor: AppColors.white,
      borderRadius: 8.w,
      backgroundColor: '#7150FC'.toColor,
      alignment: Alignment.center,
      onTap: controller.onSubmit,
      hasInkWell: false,
    );
  }
}
